<?php
/**
 * Created by PhpStorm .
 * User: {Administrator}
 * Date: {2022/7/26 14:49}
 * Author: '树叶'
 * Email: 'ayunu@qq.com'
 * Motto Info: '理想如果不向现实做一点点屈服，那么理想也将归于尘土。'
 */

namespace wxpay;
use app\admin\model\order\OrderGoods;
use app\api\model\Order;
use think\Db;
class Wxtuisong{
    protected $appId = 'wx2a66cde38cb7ab28'; //小程序id
    protected $appSecret = 'e20e541c71d7f73635437d3a9006df08'; //小程序秘钥

    /** 取消订单消息订阅
     *  $appid:微信小程序appid、$appsecret:微信小程序秘钥、$touser:用户openid、$template_id:模板id
     */
    public function sendqudingMessage($order_id,$touser,$template_id=""){
        $appid = $this->appId;
        $appsecret = $this->appSecret;
        $page = '';//跳转页面不填则模板无跳转。
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
        $res = json_decode(file_get_contents($url), true);
        $access_token = $res['access_token'];
        //请求url
        $urls = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token;
        //发送内容
        $data = [];
        $order = Order::where("id",$order_id)->find();
        $order_good = OrderGoods::where("order_id",$order_id)->select();
        // dump($order_good);
        // die;
        $good_name = "";
        foreach ($order_good as $k=>$v){
            $good_name .= $v["goods_name"].",";
        }
        $good_name = trim($good_name,",");
        //接收者（用户）的 openid
        $data['touser'] = $touser;
        //所需下发的订阅模板id
        $data['template_id'] = "6tVRXetpeUtsbeJfonGwBA4QcMDSDVznpHDqTLNuoX0";
        //点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。
        $data['page'] = $page;
        //模板内容，格式形如 { "key1": { "value": any }, "key2": { "value": any } }
        $data['data'] = [
            "thing1" => [
                'value' => $good_name
            ],
            "character_string2" => [
                'value' => $order["order_sn"]
            ],
            "amount3" => [
                'value' => $order["goods_pay_price"]
            ],
            'thing4' => [
                'value' => "取消订单"
            ],
            'time5' => [
                'value' => date("Y-m-d m:i:s",time())
            ],
        ];
        //跳转小程序类型：developer为开发版；trial为体验版；formal为正式版；默认为正式版
        $data['miniprogram_state'] = '';
        return self::curlPost($urls,json_encode($data));
    }
    /** 资质审核消息订阅
     *  $appid:微信小程序appid、$appsecret:微信小程序秘钥、$touser:用户openid、$template_id:模板id
     */
    public function sendshenheMessage($touser,$results,$notes,$template_id=""){
        $appid = $this->appId;
        $appsecret = $this->appSecret;
        $page = '';//跳转页面不填则模板无跳转。
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
        $res = json_decode(file_get_contents($url), true);
        $access_token = $res['access_token'];
        //请求url
        $urls = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token;
        //发送内容
        $data = [];
        //接收者（用户）的 openid
        $data['touser'] = $touser;
        //所需下发的订阅模板id
        $data['template_id'] = "TRCXrWd9_LbMntsalRAq6rIvuB_QkpQ_RLqnMBTDf2I";
        //点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。
        $data['page'] = $page;
        //模板内容，格式形如 { "key1": { "value": any }, "key2": { "value": any } }
        $data['data'] = [
            "phrase1" => [
                'value' => $results
            ],
            "thing3" => [
                'value' => $notes
            ],
        ];
        //跳转小程序类型：developer为开发版；trial为体验版；formal为正式版；默认为正式版
        $data['miniprogram_state'] = '';
        return self::curlPost($urls,json_encode($data));
    }
//发送post请求
    static function curlPost($url,$data)
    {
        $ch = curl_init();
        $params[CURLOPT_URL] = $url;    //请求url地址
        $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $data;
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        return $content;
    }


}

